package com.woniuxy.depot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.depot.mapper.DepotStockManageMapper;
import com.woniuxy.depot.model.DTO.StockGoodsDTO;
import com.woniuxy.depot.model.VO.PageBean;
import com.woniuxy.depot.model.VO.StockGoodsVO;
import com.woniuxy.depot.model.entity.DepotInStockItem;
import com.woniuxy.depot.model.entity.DepotStockManage;
import com.woniuxy.depot.service.DepotStockManageService;
import com.woniuxy.model.vo.DepotStockManageVo;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.NumberUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepotStockManageServiceImpl implements DepotStockManageService {
    @Resource
    private DepotStockManageMapper stockManageMapper;

    @Transactional
    @Override
    public PageBean<StockGoodsVO> searchByCondition(StockGoodsDTO stockGoodsDTO) {
        stockGoodsDTO.setIndex((stockGoodsDTO.getCurrent()-1)*stockGoodsDTO.getSize());
        PageBean<StockGoodsVO> pageBean = new PageBean<>();
        //当前页和每页条数
        pageBean.setCurrent(stockGoodsDTO.getCurrent()).setSize(stockGoodsDTO.getSize());
        //总条数和总页数
        int total = stockManageMapper.getTotal(stockGoodsDTO);
        Double d = Math.ceil((double)total / stockGoodsDTO.getSize());
        int totalPage = d.intValue();
        pageBean.setTotal(total).setTotalPage(totalPage);
        //查询商品列表
        pageBean.setList(stockManageMapper.getGoodsList(stockGoodsDTO));
        return pageBean;
    }

    @Override
    public boolean hasGoods(int depotId, String code) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("depot_id",depotId);
        condition.put("code",code);
        List<DepotStockManage> depotStockManages = stockManageMapper.selectByMap(condition);
        return !depotStockManages.isEmpty();
    }

    @Transactional
    @Override
    public void updateNum(Integer depotId, DepotInStockItem depotInStockItem) {
        DepotStockManage stockManage = new DepotStockManage().setDepotId(depotId)
                .setNum(depotInStockItem.getNum()).setCode(depotInStockItem.getItemCode());
        stockManageMapper.updateNum(stockManage);
    }

    @Transactional
    @Override
    public void addStock(Integer depotId, DepotInStockItem depotInStockItem) {
        DepotStockManage stockManage = new DepotStockManage().setDepotId(depotId)
                .setNum(depotInStockItem.getNum()).setCode(depotInStockItem.getItemCode());
        System.out.println(stockManage);
        stockManageMapper.insert(stockManage);
    }

    @Transactional
    @Override
    public boolean delStock(DepotStockManageVo stockManageVo) {
        return stockManageMapper.delStock(stockManageVo);
    }

    @Override
    public Boolean insertExcel() {
        String account = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String tableName = "库存商品表";
        return stockManageMapper.insertExcel(account,tableName);
    }


}
